package com.fzhucc.modules.lesson.service.impl;

import com.fzhucc.modules.lesson.domain.LessonIcon;
import com.fzhucc.modules.lesson.repository.LessonIconRepository;
import com.fzhucc.modules.lesson.service.LessonIconService;
import com.fzhucc.modules.lesson.service.dto.LessonIconDto;
import com.fzhucc.modules.lesson.service.dto.LessonIconQueryCriteria;
import com.fzhucc.modules.lesson.service.mapstruct.LessonIconMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author yyg
* @date 2022-12-15
**/
@Service
@RequiredArgsConstructor
public class LessonIconServiceImpl implements LessonIconService {

    private final LessonIconRepository lessonIconRepository;
    private final LessonIconMapper lessonIconMapper;

    @Override
    public Map<String,Object> queryAll(LessonIconQueryCriteria criteria, Pageable pageable){
        Page<LessonIcon> page = lessonIconRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(lessonIconMapper::toDto));
    }

    @Override
    public List<LessonIconDto> queryAll(LessonIconQueryCriteria criteria){
        return lessonIconMapper.toDto(lessonIconRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LessonIconDto findById(Integer id) {
        LessonIcon lessonIcon = lessonIconRepository.findById(id).orElseGet(LessonIcon::new);
        ValidationUtil.isNull(lessonIcon.getId(),"LessonIcon","id",id);
        return lessonIconMapper.toDto(lessonIcon);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LessonIconDto create(LessonIcon resources) {
        return lessonIconMapper.toDto(lessonIconRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LessonIcon resources) {
        LessonIcon lessonIcon = lessonIconRepository.findById(resources.getId()).orElseGet(LessonIcon::new);
        ValidationUtil.isNull( lessonIcon.getId(),"LessonIcon","id",resources.getId());
        lessonIcon.copy(resources);
        lessonIconRepository.save(lessonIcon);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            lessonIconRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LessonIconDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LessonIconDto lessonIcon : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类名称", lessonIcon.getName());
            map.put("图标url", lessonIcon.getImg());
            map.put("排序", lessonIcon.getSort());
            map.put("是否显示", lessonIcon.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}